<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员促销ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	allen.qiang
 */
quanli_require('module/base/promotion/promotionCustomer.class.php');
class ajax_promotionCustomer {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询会员促销
                    $this->searchPromotionCustomer();
                    break;
                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit', 'change'))) {
                        returnJsonData('参数错误');
                    }
                    if ($_REQUEST['act'] == 'edit') {

                        $this->savePromotionCustomer();//新增、修改会员促销

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deletePromotionCustomer();//删除会员促销

                    } else if ($_REQUEST['act'] == 'change') {

                        $this->changePromotionCustomerUseflg();//启用、禁用会员促销

                    }

                    break;
            }

        }

    }

    /**
     * 查询会员促销
     */
    private function searchPromotionCustomer() {

        global $G_PGLOB;

        //查询条件
        $promotionCustomer = new promotionCustomer( $G_PGLOB->DBCA ) ;
        $result_data	  = $promotionCustomer->searchPromotionCustomerInfo( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {

            $edit_permission = $G_PGLOB->has_permission(4517);

            foreach($result_data as &$p_c) {

                $bargainType = '' ;
                if($p_c['source'] == 1)
                    $bargainType = '注册 ' ;
                else if($p_c['source'] == 2)
                    $bargainType = '登录 ' ;
                else if($p_c['source'] == 3)
                    $bargainType = '签到 ' ;
                else if($p_c['source'] == 4)
                    $bargainType = '建议 ' ;
                else if($p_c['source'] == 5)
                    $bargainType = '问卷调查' ;
                else if($p_c['source'] == 6)
                    $bargainType = '完善资料' ;
                else if($p_c['source'] == 7)
                    $bargainType = '商品评论' ;

                $type = '';
                if($p_c['type'] == 1)
                    $type = '送积分 ' ;
                else if($p_c['type'] == 2)
                    $type = '送优惠券 ' ;

                if($edit_permission){

                    $result_array['rows'][] = array(
                        'id' 	=> $p_c['promotionID'],
                        'cell' 	=> array(
                            '<a href="javascript: void(0);" onclick="promotion.edit(' . $p_c['promotionID'] . ')">修改</a>&nbsp;' ,
                            $p_c['promotionID'] ,
                            $p_c['title'],
                            $bargainType,
                            $type,
                            $p_c['startDate'],
                            $p_c['endDate'],
                            $p_c['useFlg'] == 1 ? '<a href="javascript: void(0);" onclick="promotion.changeConfirm(' . $p_c['promotionID'] . ', 1) " >启用</a>' :
                                '<a href="javascript: void(0);" onclick="promotion.changeConfirm(' . $p_c['promotionID'] . ', 0 ) " >禁用</a>'
                        )
                    );
                }else{
                    $result_array['rows'][] = array(
                        'id' 	=> $p_c['promotionID'],
                        'cell' 	=> array(
                            $p_c['promotionID'] ,
                            $p_c['title'],
                            $bargainType,
                            $type,
                            $p_c['startDate'],
                            $p_c['endDate']
                        )
                    );
                }
            }
        }

        echo json_encode( $result_array );
    }

    /**
     * 保存会员促销信息
     *
     */
    private function savePromotionCustomer () {
        global $G_PGLOB;

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;

        $promotionCustomer 	 = new promotionCustomer( $G_PGLOB->DBCA ) ;
        $result_array 		 = $promotionCustomer->savePromotionCustomerInfo( $_REQUEST ) ;

        echo json_encode($result_array);
    }

    /**
     * 删除会员促销信息
     *
     */
    private function deletePromotionCustomer () {

        global $G_PGLOB;

        /**接收参数
         *
         * $_REQUEST[delArr] => Array(
        [0] => 19
        [1] => 18
        [2] => 17
        )
         *这是由要删除的promotionID组成的数组
         */
        $delArr = $_REQUEST['delArr'];

        //统计不存在的促销的数目
        $errorNum = 0;
        $errorArr = array();

        //记录要删除的entity对象
        $deleteObjArr = array();

        if( $delArr ){

            //检查要删除的文章是否全部存在
            foreach( $delArr as $value ){

                //检查文章是否存在
                $check_obj = new entity('promotion_customer');
                $check_obj->promotionID = $value;

                if( !$G_PGLOB->DBCA->fetch( $check_obj ) ){
                    $errorArr[] = $value;
                    $errorNum++;
                }

                $deleteObjArr[$value] = array('type'=>$check_obj->type,'delObj'=>$check_obj);

            }

            if( $errorNum > 0 ){

                echo json_encode( array('message'=>'所选部分促销已不存在，请刷新页面' , 'code'=>400 ) );
                return;

            }

            try{

                //开启事务
                $G_PGLOB->DBCA->beginTransaction();

                //循环删除
                foreach( $deleteObjArr as $key=>$delArr ){

                    $G_PGLOB->DBCA->delete( $delArr['delObj'] );

                    //如果该会员促销是赠送优惠券，则删除优惠券信息
                    if( $delArr['type'] == 2 ){

                        //删除该会员促销下的优惠券
                        $G_PGLOB->DBCA->stmt_query('DELETE FROM promotion_customer_coupon WHERE promotionID=?' , 'i' , $key);

                    }

                }

                //提交事务
                $G_PGLOB->DBCA->commit();

                echo json_encode( array('message'=>'删除成功', 'code'=>200) );
                return;

            }catch( Exception $e ){

                $G_PGLOB->DBCA->rollBack();
                error_log($e->getMessage());
                echo json_encode( array('message'=>'系统错误，请与管理员联系', 'code'=>500) );
                return;

            }

        }else{

            echo json_encode( array('message'=>'请选择要删除的促销' , 'code'=>400 ) );
            return;

        }

    }

    /**
     * 会员促销 禁用、启用
     */
    private function changePromotionCustomerUseflg() {

        global $G_PGLOB;

        $promotionID = $_REQUEST['promotionID'];
        $flg   = $_REQUEST['flg'];

        $promotion_customer_obj = new entity('promotion_customer');
        $promotion_customer_obj->promotionID = $promotionID;

        if( !$G_PGLOB->DBCA->fetch($promotion_customer_obj) ){

            echo json_encode( array('message'=>'没有找到编号['.$promotionID.']会员促销信息' , 'code'=>404 ) );
            return;

        }else{

            $userFlg = $promotion_customer_obj->useFlg;

            if( $flg == 1 && $userFlg == 0){
                echo json_encode( array('message'=>'编号['.$promotionID.']促销已处于<b>禁用</b>状态<br>请重新刷新页面' , 'code'=>400 ) );
                return;
            }

            if( $flg == 0 && $userFlg == 1){
                echo json_encode( array('message'=>'编号['.$promotionID.']促销已处于<b>启用</b>状态<br>请重新刷新页面' , 'code'=>400 ) );
                return;
            }

            if( $userFlg == 0 ){
                $promotion_customer_obj->useFlg = 1;
            }else{
                $promotion_customer_obj->useFlg = 0;
            }

        }

        try{

            $G_PGLOB->DBCA->save( $promotion_customer_obj );

            if( $userFlg == 0 ){
                echo json_encode( array('message'=>'已启用' , 'code'=>200 ) );
                return;
            }else{
                echo json_encode( array('message'=>'已禁用' , 'code'=>200 ) );
                return;
            }


        }catch(Exception $e){

            error_log( $e->getMessage() );
            echo json_encode( array('message'=>'系统错误，请与管理员联系' , 'code'=>500 ) );
            return;
        }

    }

}